
#include "widget.h"
#include "ui_widget.h"
#include <QKeyEvent>
#include "movedirection.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setFixedSize(1620,960);

    //视图场景
    mGameView.setSceneRect(QRect(0,0,1620,960));
    mScene.setSceneRect(QRect(0,0,1620,960));
    background.setPixmap(QPixmap(":/background/C:/Users/Administrator/Videos/bilibili/958376458/jhcc.jpg"));

    //箭头
    up.setPixmap(QPixmap(":/item/C:/Users/Administrator/Pictures/otto-song/direct/up.png"));
    left.setPixmap(QPixmap(":/item/C:/Users/Administrator/Pictures/otto-song/direct/left.png"));
    right.setPixmap(QPixmap(":/item/C:/Users/Administrator/Pictures/otto-song/direct/right.png"));
    down.setPixmap(QPixmap(":/item/C:/Users/Administrator/Pictures/otto-song/direct/down.png"));
    left.setPos(880,30);
    up.setPos(1040,30);
    down.setPos(1200,30);
    right.setPos(1360,30);

    //添加到场景里
    mScene.addItem(&background);
    mScene.addItem(&up);
    //mScene.addItem(&up_check);
    mScene.addItem(&left);
    mScene.addItem(&right);
    mScene.addItem(&down);
    mGameView.setScene(&mScene);
    mGameView.setParent(this);

    //设置音乐
    this->Music.setMedia(QUrl("qrc:/C:/Users/Administrator/Videos/otto jhcc/otto.mp3"));
    this->Music.setVolume(30);
    this->Music.play();

    //开启按键检测定时器
    DirectPointTimer = new QTimer(this);
    DirectPointTimer->start(1);
    connect(DirectPointTimer,&QTimer::timeout,this,&Widget::DirectChange);

    //开启箭头创建定时器
    DirectMakeTimer = new QTimer(this);
    DirectMakeTimer->start(500);//每隔这样一个速度调用函数
    connect(DirectMakeTimer,&QTimer::timeout,this,&Widget::MakeDirection);

    //开启箭头移动
    DirectMoveTimer = new QTimer(this);
    DirectMoveTimer->start(10);
    connect(DirectMoveTimer,&QTimer::timeout,[this](){
        for(auto movedirection : UPsList)
        {
            movedirection->DMove();
        }

        //collision();
    });

    //开启按键检测
    DirectCheckTimer = new QTimer(this);
    DirectCheckTimer->start(1);
    connect(DirectCheckTimer,&QTimer::timeout,this,&Widget::collision);

}

Widget::~Widget()
{
    delete ui;
}

//碰撞检测
void Widget::collision()
{
    QPoint pos(1040,30);
    for(int i =0;i<UPsList.size();i++)
    {
        if(UPsList[i]->contains(up.pos()))
        {
            mScene.removeItem(UPsList[i]);
            delete UPsList[i];
            UPsList.removeOne(UPsList[i]);
        }
    }
}

//创建移动箭头
void Widget::MakeDirection()
{
    //创建箭头up
    QPixmap upImg(":/item/C:/Users/Administrator/Pictures/otto-song/direct/up_point.png");
    QPoint pos(1040,800);
    MoveDirection* move_up =new MoveDirection(pos,upImg,MoveDirection::DI_up);
    //添加到场景
    mScene.addItem(move_up);
    //添加到管理器
    UPsList.append(move_up);
}



//按按键后箭头变色
void Widget::keyPressEvent(QKeyEvent *event)
{
    //添加按键到按键 组合
    switch(event->key()){
    case Qt::Key_W:
    case Qt::Key_A:
    case Qt::Key_S:
    case Qt::Key_D:
        KeyList.append(event->key());
        break;
    }
}

void Widget::DirectChange()
{
    for(int keyCode : KeyList)
    {
        switch(keyCode)
        {
        case Qt::Key_W: up.setPixmap(QPixmap(":/item/C:/Users/Administrator/Pictures/otto-song/direct/up_point.png"));break;
        case Qt::Key_A: left.setPixmap(QPixmap(":/item/C:/Users/Administrator/Pictures/otto-song/direct/left_point.png"));break;
        case Qt::Key_S: down.setPixmap(QPixmap(":/item/C:/Users/Administrator/Pictures/otto-song/direct/down_point.png"));break;
        case Qt::Key_D: right.setPixmap(QPixmap(":/item/C:/Users/Administrator/Pictures/otto-song/direct/right_point.png"));break;
        }
    }
}

void Widget::keyReleaseEvent(QKeyEvent *event)
{
    //移除对应按键组合
    if(KeyList.contains(event->key()))
    {
        KeyList.removeOne(event->key());
    }
    switch(event->key()){
    case Qt::Key_W: up.setPixmap(QPixmap(":/item/C:/Users/Administrator/Pictures/otto-song/direct/up.png"));break;
    case Qt::Key_A: left.setPixmap(QPixmap(":/item/C:/Users/Administrator/Pictures/otto-song/direct/left.png"));break;
    case Qt::Key_S: down.setPixmap(QPixmap(":/item/C:/Users/Administrator/Pictures/otto-song/direct/down.png"));break;
    case Qt::Key_D: right.setPixmap(QPixmap(":/item/C:/Users/Administrator/Pictures/otto-song/direct/right.png"));break;
    }
}